package com.sg.hms.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Pagebean;
import com.sg.hms.Model.Room;
import com.sg.hms.Service.RoomService;
import com.sg.hms.ServiceImpl.RoomServiceImpl;
/***
 * 通过门牌号获得房间信息如果门牌号为空 则返回所有房间
 * @author 吴磊
 *
 */
public class SearchRoomController extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		String pString = req.getParameter("page");
		// 页码处理页码为空的时候默认为一
		int p = 0;
		if (pString == null) {
			p = 1;
		} else {
			p = Integer.parseInt(pString);
		}
		Pagebean pagebean=new Pagebean(p, 5);
		String key1= req.getParameter("key");
		if ((key1==null)==false) {
			req.getSession().setAttribute("key", key1);
		}
		RoomService room=new RoomServiceImpl();
		String key=(String) req.getSession().getAttribute("key");
		ArrayList<Room> rooms= room.getRoom(key);
		int size=rooms.size();
		if (size>5) {
			req.setAttribute("page", pagebean.getthisPage1(size,p,pagebean.getPageSize()));
		}
		else {
			req.setAttribute("page", null);
		}
		ArrayList<Room> rooms1=room.getRoom(pagebean, key);
		req.setAttribute("rooms",rooms1);
		req.getRequestDispatcher("page/room.jsp").forward(req, resp);
	}

}
